package edu.mobbuzz.page.menu.category;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.bean.Category;
import edu.mobbuzz.facade.CategoryFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;

public class CategoryForm extends Menu {
    private Form categoryForm;
    private Container categoryPanel;
    private Label menuTitle;
    private Label nameLbl, descriptionLbl;
    private TextField nameTxt;
    private TextArea descriptionArea;    
    private Command backCmd, okCmd;
    private Category category;
    private int mode;
    private int index;
    private CategoryFacade categoryFacade;
    
    public CategoryForm() {
        categoryForm = new Form();
        categoryPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        nameLbl = new Label();
        nameTxt = new TextField();
        descriptionLbl = new Label();
        descriptionArea = new TextArea(5, 20);
        
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToCategoryPage();
            }
        };
        
        okCmd = new Command("Ok") {
            public void actionPerformed(ActionEvent evt) {
                saveCategory();
            }
        };
        
        
    }
    
    public void showPage() {
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);  
        
        nameLbl.setText("Label :");
        nameLbl.getStyle().setMargin(5, 0, 10, 2);
        nameLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        nameLbl.getStyle().setBgTransparency(0);  
        
        nameTxt.getStyle().setMargin(2, 0, 10, 10);
        
        descriptionLbl.setText("Description :");
        descriptionLbl.getStyle().setMargin(2, 0, 10, 10);
        descriptionLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        descriptionLbl.getStyle().setBgTransparency(0);

        descriptionArea.getStyle().setMargin(2, 0, 10, 10);
        
        if(getCategory()!=null){
            nameTxt.setText(getCategory().getLabel());
            descriptionArea.setText(getCategory().getDescription());
        }
        
        categoryPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));        
        categoryPanel.addComponent(menuTitle);
        categoryPanel.addComponent(nameLbl);
        categoryPanel.addComponent(nameTxt);
        categoryPanel.addComponent(descriptionLbl);
        categoryPanel.addComponent(descriptionArea);
        
        categoryForm.setLayout(new BorderLayout());
        categoryForm.addComponent(BorderLayout.CENTER, categoryPanel);
        categoryForm.addCommand(backCmd);
        categoryForm.addCommand(okCmd);
        categoryForm.setCommandListener(getActionlistener());
        categoryForm.show();
    }
    
    public void backToCategoryPage(){
        CategoryPage categoryPage = new CategoryPage();
        categoryPage.setExitCmd(getExitCmd());
        categoryPage.setActionlistener(getActionlistener());
        categoryPage.showPage();
    }
    
    public void saveCategory(){
        if(nameTxt.getText()==null||nameTxt.getText().equals("")){
            Dialog.show("Error", "Label must not be left blank", Dialog.TYPE_ERROR, null, "Ok", null);
        }else{
            Category c = null;
            if(getMode()==ModeString.NEW){
                c = new Category();
            }else if(getMode()==ModeString.EDIT){
                c = getCategory();
            }

            c.setLabel(nameTxt.getText());
            c.setDescription(descriptionArea.getText());

            if(getMode()==ModeString.NEW){
                getCategoryFacade().addCategory(c);
            }else if(getMode()==ModeString.EDIT){
                getCategoryFacade().updateCategory(getIndex(), c);
            }

            CategoryPage categoryPage = new CategoryPage();
            categoryPage.setExitCmd(getExitCmd());
            categoryPage.setActionlistener(getActionlistener());
            categoryPage.showPage();
        }
        
    }
       
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
    
    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public CategoryFacade getCategoryFacade() {
        if(categoryFacade==null){
            categoryFacade = new CategoryFacade();
        }
        return categoryFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.CATEGORY;

    }
 
}
 
